#!/bin/python
# -*- coding: utf-8 -*-
"""
Name: sdk.py
Author: harrybao
Mail: harrybao@123u.com
Time: 2021/07/08 09:36
Desc: SDK脚本
"""

import os
import sys
import getopt
from path_util import PathUtil
from thirdparty_builder import ThirdPartyBuilder
from cpp_builder import CppBuilder
from cpp_packer import CppPacker

def init():
    """初始化 """
    path_util = PathUtil()

    os.makedirs(path_util.sdk_thirdparty_path, exist_ok=True)
    os.chdir(path_util.sdk_thirdparty_path)

    thirdparty_builder = ThirdPartyBuilder()

    if not thirdparty_builder.init_rpc_backend_cpp():
        return False
    
    if not thirdparty_builder.init_knet():
        return False
 
    return True

def build_sdk():
    """构建SDK"""
    sdk_builder = CppBuilder()
    if not sdk_builder.generate_cmake():
        return False
    if not sdk_builder.build_sdk():
        return False
    return True

def pack_sdk():
    """打包SDK"""
    include_path_list = []
    CppBuilder().get_include_path_list(include_path_list)
    # print(include_path_list)
    CppPacker().pack(include_path_list)
    pass

def clean_package(service_list):
    """清理package目录"""
    path_util = PathUtil()
    for service_name in service_list:
        path_util.remove_path(os.path.join(path_util.sdk_package_doc_path, service_name), False)
        path_util.remove_path(os.path.join(path_util.sdk_package_include_path, 'proxy', service_name), False)
        path_util.remove_path(os.path.join(path_util.sdk_package_include_path, 'struct', service_name), False)
        path_util.remove_path(os.path.join(path_util.sdk_package_lib_path, service_name), False)
        path_util.remove_path(os.path.join(path_util.sdk_package_protobuf_path, service_name), False)
        path_util.remove_path(os.path.join(path_util.sdk_package_proxy_path, service_name), False)
        path_util.remove_path(os.path.join(path_util.sdk_package_stub_path, service_name), False)
        path_util.remove_path(os.path.join(path_util.sdk_package_info_path, service_name), False)
 
def build_demo():
    """构建Demo"""
    CppBuilder().build_demo()

def build_rpc():
    """编译rpc-backend-cpp"""
    if not ThirdPartyBuilder().build_rpc_backend_cpp():
        return False
    return True

def build_knet():
    """编译knet"""
    if not ThirdPartyBuilder().build_knet():
        return False
    return True


def usage():
    """使用方式"""
    print("")
    print("    -h,--help                               使用说明")
    print("    --init                                  初始化")
    print("    --build_sdk                             构建SDK")
    print("    --build_demo                            构建demo")
    print("    --build_rpc                             构建rpc")
    print("    --build_knet                            构建knet")
    print("    --pack_sdk                              打包SDK")
    print("    --clean_package                         清理package目录")
 

def parse_options():
    """解析参数，执行命令"""
    try:
        opts, args = getopt.getopt(
            sys.argv[1:], '-h', ['help', 'init', 'build_sdk', 'build_demo', 'build_rpc', 'build_knet', 'pack_sdk', 'clean_package='])
    except getopt.GetoptError as e:
        print(str(e))
        return

    if len(opts) == 0:
        usage()
        return

    for opt, value in opts:
        if opt in ('-h', '--help'):
            usage()
        if opt in ('--init'):
            init()
        if opt in ('--build_sdk'):
            build_sdk()
        if opt in ('--build_demo'):
            build_demo()
        if opt in ('--build_rpc'):
            build_rpc()
        if opt in ('--build_knet'):
            build_knet()
        if opt in ('--pack_sdk'):
            pack_sdk()
        if opt in ('--clean_package'):
            service_list = []
            service_list.append(value)
            clean_package(service_list)
    
if __name__ == "__main__":
    parse_options()
